package com.itheima.maplearn;

import java.util.Set;
import java.util.TreeSet;

public class ComparableTest {
    public static void main(String[] args){
        Wugui wg1=new Wugui("wg1",10);
        Wugui wg2 =new Wugui("wg2",12);

        Set<Wugui> tests = new TreeSet<>();
        tests.add(wg1);
        tests.add(wg2);

        for(Wugui wg:tests){
            //toString改写过了
            System.out.println(wg);
        }

    }




}

//方法一：自建类实现java.lang.Comparable接口
class Wugui implements Comparable<Wugui>{
    private String name;
    private int age;

    public Wugui(String name,int age){
        this.name= name;
        this.age=age;
    }

    public String toString(){
        return "name:"+name+" age:"+age;
    }

    @Override
    public int compareTo(Wugui o){
       return this.age-o.age;
    }


}